package com.youlu.campus.website.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.website.entity.CompanyDynamicDetail;
import com.youlu.campus.website.entity.CompanyDynamicDetailPageQuery;
import com.youlu.campus.website.service.CompanyDynamicDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公司动态详情
 */
@RestController
@RequestMapping(value = "/website/company/dynamic")
public class CompanyDynamicDetailController {

    @Autowired
    private CompanyDynamicDetailService companyDynamicDetailService;


    /**
     * 新增/修改动态详情
     *
     * @param detail
     * @return
     */
    @PostMapping("/save")
    public QResult<CompanyDynamicDetail> save(@RequestBody CompanyDynamicDetail detail) {
        if (StringUtils.isAnyBlank(detail.getTitle(), detail.getContent(), detail.getType())) {
            return QResult.fail("参数错误");
        }
        return QResult.success(companyDynamicDetailService.save(detail));
    }

    /**
     * 根据 ID 查询动态详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public QResult<CompanyDynamicDetail> findById(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return QResult.fail("参数错误");
        }
        return QResult.success(companyDynamicDetailService.findById(id));

    }


    /**
     * 根据 ID 删除动态详情
     *
     * @param detail
     */
    @PostMapping("/delete")
    public QResult delete(@RequestBody CompanyDynamicDetail detail) {
        if (StringUtils.isBlank(detail.getId())) {
            return QResult.fail("参数错误");
        }
        companyDynamicDetailService.delete(detail.getId());
        return QResult.success();

    }

    /**
     * 分页 查询动态详情
     *
     * @param pageQuery
     * @return
     */
    @PostMapping("/find-page")
    public QResult<Page<CompanyDynamicDetail>> findByPage(@RequestBody CompanyDynamicDetailPageQuery pageQuery) {
        return QResult.success(companyDynamicDetailService.findByPage(pageQuery));
    }
}